/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerUserControllerImpl
 * Author:   Allen
 * Date:     2020/8/27 15:28
 * Description: 商家/系统用户信息处理实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.bim.marvel.openapi.client.constant.OpenApiCodeEnum;
import com.bim.marvel.openapi.client.dto.DealerUserDTO;
import com.bim.marvel.openapi.client.enums.QueryTypeEnum;
import com.bim.marvel.openapi.client.query.DealerUserQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.openapi.core.service.impl.DealerUserService;
import com.bim.marvel.rest.DealerUserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈商家/系统用户信息处理实现类〉
 *
 * @author Allen
 * @date 2020/8/27
 * @since 1.0.0
 */
@RestController
public class DealerUserControllerImpl implements DealerUserController {

    /**
     * 注入DealerUserService
     */
    @Autowired
    private DealerUserService dealerUserService;

    @Override
    public SimpleVO<Boolean> saveDealerUser(DealerUserQuery dealerUserQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = dealerUserService.saveDealerUser(dealerUserQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.INSERT_FAILED);
        }
        return simpleVO;
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @Override
    @Cacheable(value = "getDealerUserInfo", key = "'userInfo:' + #loginName")
    public SimpleVO<DealerUserDTO> getDealerUserInfo(
            @AuthenticationPrincipal String loginName) {
        DealerUserDTO userInfoDTO = dealerUserService.getUserByLoginName(loginName);
        if (userInfoDTO == null) {
            return new SimpleVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        return new SimpleVO<>(userInfoDTO);
    }

    @Override
    public SimpleVO<Boolean> getLoginNameExist(String loginName) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        Boolean success = dealerUserService.getLoginNameOrPhoneExist(loginName, QueryTypeEnum.LONGIN_NAME);
        if (success) {
            simpleVO = new SimpleVO(OpenApiCodeEnum.LOGIN_NAME_EXISR);
        }
        return simpleVO;
    }

    @Override
    public SimpleVO<Boolean> getPhoneExist(String phone) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        Boolean success = dealerUserService.getLoginNameOrPhoneExist(phone, QueryTypeEnum.PHONE);
        if (success) {
            simpleVO = new SimpleVO(OpenApiCodeEnum.PHONE_EXISR);
        }
        return simpleVO;
    }
}
